<template>
<div>

  <div class='showlogin' v-show='this.$store.state.login.flag'>
    <div class='top'>
      <span>登录</span>
      <span @click='off'> x </span>
    </div>



    <div class='logincontent' v-if='logins'>
      <div class='phone'></div>
      <div class='right qrcode'>
        <div class='title'>扫码登录</div>
        <div class='qrcode' >
          <img src='' alt='' ref='qrCodeUrl'>
        </div>
      </div>
      <div class='footer'>
        <a href='javascript:;' @click='login'>选怎其他登录模式</a>
        <p class='txt'>使用&nbsp;
          <a>网易云音乐APP</a>&nbsp;扫码登录
        </p>
      </div>
    </div>

    <div class='logincontent live' v-else>
      <form action=''>
        <div class='phones'>
          <label for='phone'>手机号:</label>
          <span>
<!--            <input v-model='phone' type='number' id='phone'>-->
            <el-input placeholder='请输入手机号' v-model='phone' clearable id='phone'></el-input>
          </span>
        </div>

        <div class='word'>
          <label for='word'>密码:</label>
          <span>
<!--            <input v-model='password' type='password' id='word'>-->
                <el-input placeholder='请输入密码'  v-model='password' show-password id='word'></el-input>
          </span>
        </div>
<!--        <button @click.prevent='getlogins'>登录</button>-->
        <el-button type='primary' @click.prevent='getlogins'>登录</el-button>
      </form>
      <div class='zhuce'>
        <a href=''>没有账号?点击注册</a>
      </div>

      <div class='footers'>
        <a href='javascript:;' @click='login'>选怎其他登录模式</a>
      </div>
    </div>


  </div>



</div>
</template>

<script>
import Config from '@/network/config'
import axios from 'axios'
import QRCode from 'qrcodejs2'
export default {
  name: 'Login',
  data() {
    return {
      phone: '',
      password: '',
      logins: true,
      url: Config.url,
      timer:null,
    }
  },

  methods: {
    getlogins() {
      if (this.phone === '' || this.password === '') {
        this.$message.error('请输入账号和密码')
      } else {
        axios.get(`${this.url}/login/cellphone?`, {
          params: {
            phone: this.phone,
            password: this.password
          },
          withCredentials: true,
        }
      ).then(res => {
          if (res.data.code === 200) {
            this.off()//关闭登录框
            window.localStorage.setItem('token',res.data.token)
            this.$store.commit('user/setUser', res.data)//登录成功后吧配置信息保存到 vuex 中
            this.$store.commit('user/settoken', JSON.stringify(res.data.token))//登录成功后吧配置信息保存到 vuex 中
            this.$store.commit('user/login')
            this.$router.go(-1)
            this.$message.success('登录成功')
          } else if (res.data.code === 502) {
            this.$message.error('密码错误')
          } else if (res.data.code === 400) {
            this.$message.error('账号不存在')
          }
        })
      }
    },
    login() {
      this.logins = !this.logins
      if(this.logins){
        this.loginsss()
      }
    },//登录框切换

   // 二维码检测扫码状态接口
    async checkStatus(key) {
      const res = await axios({
        url: `${this.url}/login/qr/check?key=${key}&timerstamp=${Date.now()}`,
        withCredentials: true, //关键
      })
      return res.data
    },

    //登录状态检测
    async  getLoginStatus() {
      const res = await axios({
        url: `${this.url}/login/status?timerstamp=${Date.now()}`,
        withCredentials: true, //关键
      })
    },

    async loginsss() {
      let timestamp = Date.now()
      this.getLoginStatus()
      const res = await axios({
        url: `${this.url}/login/qr/key?timerstamp=${Date.now()}`,
        withCredentials: true, //关键
      })
      const key = res.data.data.unikey
      const res2 = await axios({
        url: `${this.url}/login/qr/create?key=${key}&qrimg=true&timerstamp=${Date.now()}`,
        withCredentials: true, //关键
      })
      const code = res2.data.data.qrimg;  //将二维码保存到图片上
      this.$refs.qrCodeUrl.src = code;


      this.timer = setInterval(async () => {
        const statusRes = await this.checkStatus(key)
        if (statusRes.code === 800) {
          this.$message.error('二维码已过期,请重新获取')
          clearInterval(this.timer)
        }
        if (statusRes.code === 803) {
          // this.$store.commit('user/setUser', res.data.data.profile)//登录成功后吧配置信息保存到 vuex 中
          // 这一步会返回cookie
          clearInterval(this.timer)
          this.off();
          this.$message.success('授权成功')
          this.$router.push('/home')
          // await this.getLoginStatus()
          this.$router.go(-1)
          //登录成功后在检测一下状态
          const res = await axios({
            url: `${this.url}/login/status?timerstamp=${Date.now()}`,
            withCredentials: true, //关键
          })
          this.$store.commit('user/setUser',res.data.data)//登录成功后吧配置信息保存到 vuex 中
          this.$store.commit('user/login')
        }
      }, 3000)
    },
    off() {
      this.$store.commit('login/show')
      this.move()
    },//关闭登录框
    move() {
      var mo = function(e) {
        e.preventDefault()
      }
      document.body.style.overflow = ''//出现滚动条
      document.removeEventListener('touchmove', mo, false)
    },//滚动事件
    go() {
      var divs = document.querySelector('.showlogin')
      //元素的鼠标落下事件
      divs.onmousedown = function(ev) {
        //event的兼容性
        var ev = ev || event

        //获取鼠标按下的坐标
        var x1 = ev.clientX
        var y1 = ev.clientY

        //获取元素的left，top值
        var l = divs.offsetLeft
        var t = divs.offsetTop

        //给可视区域添加鼠标的移动事件
        document.onmousemove = function(ev) {

          //event的兼容性
          var ev = ev || event

          //获取鼠标移动时的坐标
          var x2 = ev.clientX
          var y2 = ev.clientY

          //计算出鼠标的移动距离
          var x = x2 - x1
          var y = y2 - y1

          //移动的数值与元素的left，top相加，得出元素的移动的距离
          var lt = y + t
          var ls = x + l

          //更改元素的left，top值
          divs.style.top = lt + 'px'
          divs.style.left = ls + 'px'

        }
        //清除
        document.onmouseup = function(ev) {
          document.onmousemove = null
        }
      }
    }  //登录框移动事件

  },
  created() {
    this.loginsss()
  },
  updated() {
    // this.$store.getters['user/getdata']//获取 session中用户的登录状态
  },
  destroyed(){
    clearInterval(this.timer)
  }

}
</script> 

<style scoped>


.conentfooter li {
  float: left;
  height: 40px;
  padding: 0 18px;
  border-right: 1px solid #e4e4e4;
}

.conentfooter li a {
  display: block;
  color: #666;
}

.conentfooter li strong {
  display: block;
  font-size: 20px;
  font-weight: normal;
}


.btn a {
  color: #fff;
  text-decoration: none;
  padding: 0 5px 0 0;
  white-space: nowrap;
  display: inline-block;
  *display: inline;
  *zoom: 1;
  height: 31px;
  line-height: 31px;
  overflow: hidden;
  *line-height: 32px;
  vertical-align: top;
  text-align: center;
  cursor: pointer;
  background: url(../../assets/images/button2.png)right -428px no-repeat;
}

.btn a i {
  width: 60px;
  color: #fff;
  background-position: 0 -387px !important;
  padding: 0 15px 0 20px;
  pointer-events: none;
  display: inline-block;
  *display: inline;
  *zoom: 1;
  height: 31px;
  line-height: 31px;
  overflow: hidden;
  *line-height: 32px;
  vertical-align: top;
  text-align: center;
  cursor: pointer;
  background: url(../../assets/images/button2.png) no-repeat;
}



.u-lv i {
  float: right;
}



.conentright p {
  width: 100%;
  margin-top: 5px;
}



form button {
  /*background-color: salmon;*/
  /*border: none;*/
  width: 40%;
  height: 30px;
  margin-left: 32px;
  /*border-radius: 5px;*/
  margin-top: 20px;
  line-height: 5px;
}

.zhuce {
  margin-top: 20px;
}

.zhuce a {
  width: 10px;
  height: 20px;
  color: black;
  margin-top: 10px;
  margin-left: 37px;
}

.footers a {
  display: block;
  width: 118px;
  height: 100%;
  margin: 20px auto 0;
  padding-right: 0;
  font-size: 12px;
  border: 1px solid #979797;
  border-radius: 15px;
  line-height: 28px;
  text-align: center;
  color: rgba(0, 0, 0, 0.8);
}

.footers {
  position: absolute;
  bottom: -16.3%;
  left: 38.7%;
}

.live span {
  width: 60%;
  margin-top: 10px;
}

label {
  padding-top: 18px;
  margin-bottom: 0;
  width: 21%;
  text-align: center;
}

span input {
  display: block;
  width: 100%;
  height: 34px;
  padding: 6px 12px;
  font-size: 14px;
  line-height: 1.42857143;
  color: #555;
  background-color: #fff;
  background-image: none;
  border: 1px solid #ccc;
  border-radius: 4px;
  -webkit-box-shadow: inset 0 1px 1px rgb(0 0 0 / 8%);
  box-shadow: inset 0 1px 1px rgb(0 0 0 / 8%);
  -webkit-transition: border-color ease-in-out .15s, -webkit-box-shadow ease-in-out .15s;
  -o-transition: border-color ease-in-out .15s, box-shadow ease-in-out .15s;
  transition: border-color ease-in-out .15s, box-shadow ease-in-out .15s;
}

.word {
  width: 100%;
  text-align: center;
  display: flex;
  align-items: center;
}

.phones {
  width: 100%;
  text-align: center;
  display: flex;
  align-items: center;
}

form {
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100%;
  justify-content: center;
  align-items: center;
}

.live {
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.live > div {
  text-align: center;
}

.logincontent .footer > a {
  display: block;
  width: 118px;
  height: 100%;
  margin: 20px auto 0;
  padding-right: 0;
  font-size: 12px;
  border: 1px solid #979797;
  border-radius: 15px;
  line-height: 28px;
  text-align: center;
  color: rgba(0, 0, 0, 0.80);
}

.logincontent .footer {
  position: absolute;
  bottom: -27px;
  left: 0;
  text-align: center;
  width: 100%;
}

.txt a {
  color: #0c73c2;
  cursor: pointer;
}

.txt {
  font-size: 12px;
  width: 200px;
  left: 260px;
  bottom: 56px;
  line-height: 17px;
  color: rgba(0, 0, 0, 0.4);
  position: absolute;
}

.qrcode .title {
  text-indent: 1rem;
}

.qrcode {
  margin: 0px 0 0 16px;
}

.logincontent .right {
  margin-left: 25px;
  text-align: center;
}

.phone {
  transition: width 10s linear;
  margin-left: 101px;
  width: 125px;
  height: 220px;
  background: url('../../assets/images/qr_guide.png');
  background-size: contain;
  transition: 2s all linear;
}

.logincontent {
  width: 100%;
  display: flex;
  padding: 38px 0 20px;
  position: relative;
}

.showlogin .top {
  display: flex;
  justify-content: space-between;
  z-index: 10;
  border-bottom: 1px solid #191919;
  border-radius: 4px 4px 0 0;
  background: #2d2d2d;
  height: 39px;
  align-items: center;
  cursor: move;
}

.showlogin .top span:nth-child(1) {
  margin-left: 20px;
}

.showlogin .top span:nth-child(2) {
  margin-right: 20px;
  cursor: pointer;
}

.showlogin .top span {
  color: white;
}

.showlogin {
  -moz-user-select: none;
  -webkit-user-select: none;
  user-select: none;
  position: absolute;
  z-index: 9998;
  width: 530px;
  border-radius: 4px;
  box-shadow: 0 5px 16px rgb(0 0 0);
  background: #fff;
  border: none;
  top: 275px;
  left: 695px;
  height: 372px;
  overflow: hidden;
}


.recommendhot {
  width: 982px;
  margin: 0 auto;
  display: flex;
  font-size: 12px;
  box-sizing: border-box;
}

.recommendhot > .right {
  background-color: #fff;
  width: 250px;
  border-left: 1px solid #eee;
}



.content ul {
  display: flex;
}

.content .text {
  width: 100%;
  display: flex;
  border-bottom: 2px solid #C10D0C;
  background: url("../../assets/images/index.png") -225px -156px no-repeat;
}

.hot p {
  font-size: 20px;
  font-weight: normal;
  line-height: 28px;
  text-indent: 2rem;
  margin-bottom: 10px;

}

.text ul {
  margin-left: 20px;
  display: flex;
  align-items: center;
}

.text ul li span {
  margin: 0 10px
}



.more i {
  background: url("../../assets/images/index.png") 0 -240px no-repeat;
  display: inline-block;
  width: 12px;
  height: 12px;
}


.pic img {
  overflow: hidden;

}

.pic img:hover {
  transform: scale(1.2);
  transition: 0.8s all linear;

}

.pic ul li {
  overflow: hidden;
}

.pic ul {
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
  overflow: hidden;

}

.pic ul li {
  width: 140px;
  height: 204px;
  position: relative;
}

.pic p {
  margin: 8px 0 3px;
  font-size: 14px;
}



.bottom .icon {
  float: left;
  width: 14px;
  height: 11px;
  margin: 9px 5px 9px 10px;
  background: url("../../assets/images/iconall.png") no-repeat 0 -24px;
}


.bottom a:hover {
  background-position: 0 -60px !important;
}

.bottom a {
  position: absolute;
  background: url("../../assets/images/iconall.png");
  right: 10px;
  bottom: 5px;
  width: 16px;
  height: 17px;
}

.pic img {
  width: 140px;
  height: 140px;
}



.bottomc a:hover {
  background-position: 0 -60px !important;
}

.bottomc a {
  position: absolute;
  background: url("../../assets/images/iconall.png");
  right: 10px;
  bottom: 5px;
  width: 16px;
  height: 17px;
}


.main .mainconent2 > p {
  width: 205px;
  margin: 0 auto;
  padding: 16px 0;
  line-height: 22px;
  color: #666;
  font-size: 12px;
}

.main .mainconent2 > button {
  display: block;
  width: 100px;
  height: 31px;
  line-height: 31px;
  text-align: center;
  color: #fff;
  text-shadow: 0 1px 0 #8a060b;
  margin: 0 auto;
  background: url("../../assets/images/index.png") 0 -195px no-repeat;
  border: none;
  outline: none;
  cursor: pointer;
}

.main > button:hover {
  color: #fff;
  text-shadow: none;
}



.songer .title {
  display: flex;
  justify-content: space-between;
  height: 23px;
  margin: 0 20px;
  border-bottom: 1px solid #ccc;
  color: #333;
}

.songer .title a {
  display: block;
  font-weight: normal;
  color: #666;
}

.songer .title a:hover {
  text-decoration: underline;
}


.list li {
  margin-top: 14px;
  float: left;
  width: 210px;
  height: 62px;
  background: #fafafa;
  text-overflow: ellipsis;
  white-space: nowrap;
}


.list .right {
  float: left;
  width: 133px;
  height: 60px;
  padding-left: 14px;
  border: 1px solid #e9e9e9;
  border-left: none;
  display: flex;
  flex-direction: column;
  justify-content: space-around;
}

.list .right p {
  color: #666;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}


.list .left img:hover {
  transform: scale(1.5);
  transition: all .3s linear;
}



.beto a {
  width: 210px;
  margin: 0 auto;
  height: 31px;
  display: block;
  text-align: center;
  line-height: 31px;
  outline: none;
  cursor: pointer;
  border-radius: 4px;
  background: url("../../assets/images/button2.png") right -100px;
}

.beto a i {
  width: 98%;
  height: 100%;
  background: url("../../assets/images/button2.png") 0 -59px;
  display: block;
  font-style: normal;
}



.anchor li {
  float: left;
  width: 210px;
  height: 50px;
}

.anchor li > a {
  float: left;
  width: 40px;
  margin-right: 10px;
  cursor: pointer;
}


.anchor .info p {
  text-overflow: ellipsis;
  white-space: nowrap;
  color: #666;
}

.anchor img {
  width: 40px;
  height: 40px;
}



.bangdan .top {
  height: 100%;
  width: 230px;
}



.logo .left img {
  width: 100%;
  height: 100%;
}

.logo .left {
  float: left;
  display: inline;
  width: 80px;
  height: 80px;
  position: relative;
}

.logo .right {
  float: left;
  width: 116px;
  margin: 6px 0 0 10px;
}

.logo .right > a {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  display: block;
  background-color: #F4F4F4;
  font-size: 14px;
  cursor: pointer;
  font-weight: bold;
}

.logo .right > a:hover {
  text-decoration: underline;
  color: black;
}

.logo .right .btn {
  margin-top: 10px;
}

.logo .right .btn a:nth-child(1) {
  display: block;
  float: left;
  width: 22px;
  height: 22px;
  margin-right: 10px;
  text-indent: -9999px;
  background: url("../../assets/images/index.png") -267px -205px no-repeat;
}

.logo .right .btn a:nth-child(2) {
  display: block;
  float: left;
  width: 22px;
  height: 22px;
  margin-right: 10px;
  text-indent: -9999px;
  background: url("../../assets/images/index.png") -300px -205px no-repeat;
}



.song li {
  height: 32px;
}

.song li:nth-child(1) span {
  color: #c10d0c;
}

.song li:nth-child(2) span {
  color: #c10d0c;
}

.song li:nth-child(3) span {
  color: #c10d0c;
}

.song span {
  float: left;
  position: relative;
  width: 35px;
  height: 32px;
  margin-left: -35px;
  text-align: center;
  color: #666;
  font-size: 16px;
}

ol li a {
  color: #000;
}
</style>